/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : TabMenuServiceFactory.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 9, 2011  mycodetalks			       Created
 */
package net.lwf.jaxb.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;

import net.lwf.entity.KnowledgeResource;
import net.lwf.jaxb.service.tabmenu.TabMenuService;
import net.lwf.jaxb.stubs.tabmenu.LinkInfo;
import net.lwf.jaxb.stubs.tabmenu.Resource;
import net.lwf.jaxb.stubs.tabmenu.SubjectInfo;
import net.lwf.jaxb.stubs.tabmenu.TopicInfo;


/**
 * Class description goes here
 */
public class TabMenuServiceFactory {
	
	private static TabMenuServiceFactory instance = null;
	private Map<String, List<SubjectInfo>> tabMenuInfo;
	private Map<String, KnowledgeResource> knowledgeresources;
	

	private TabMenuServiceFactory() {
	}

	public synchronized final static TabMenuServiceFactory getInstance(){
		if (instance == null) {
			instance = new TabMenuServiceFactory();
		}
		return instance;
	}

	public TopicInfo getMenu(String in_grade, String in_subject, String in_topic) {
		List<TopicInfo> topics = getMenu(in_grade, in_subject).getTopicInfo();
		for(TopicInfo topic: topics) {
			if(topic.getName().equalsIgnoreCase(in_topic)) {
				return topic;
			}
		}
 		return null;
	}
	
	public SubjectInfo getMenu(String in_grade, String in_subject) {
		List<SubjectInfo> subjects = getMenu(in_grade);
		for(SubjectInfo subject: subjects) {
			if(subject.getName().equalsIgnoreCase(in_subject)) {
				return subject;
			}
		}
		return null;
	}
	
	public List<SubjectInfo> getMenu(String in_grade) {
		if(tabMenuInfo == null || !tabMenuInfo.containsKey(in_grade)) {
			tabMenuInfo = new TreeMap<String, List<SubjectInfo>>();
			TabMenuService service = new TabMenuService();
			tabMenuInfo.put(in_grade, service.getMenu(in_grade));
		}
		return tabMenuInfo.get(in_grade);
	}
	
	public KnowledgeResource getResources(String in_grade, String in_subject, String in_topic, String operation) {
		KnowledgeResource resource = null;
		if(StringUtils.isNotEmpty(in_grade)   && 
		   StringUtils.isNotEmpty(in_subject) && 
		   StringUtils.isNotEmpty(in_topic)   && 
		   StringUtils.isNotEmpty(operation)) {
			
			
			String key = in_grade + in_subject + in_topic + operation;
			if(this.knowledgeresources == null) {
				this.knowledgeresources = new HashMap<String, KnowledgeResource>();
			}
			if(!this.knowledgeresources.containsKey(key)) {
				TopicInfo topicinfo = this.getMenu(in_grade, in_subject, in_topic);
				if(topicinfo != null) {
					List<LinkInfo> linkinfo = topicinfo.getLinkInfo();
					for(LinkInfo link_info: linkinfo) {
						if(link_info.getName().equalsIgnoreCase(operation)) {
							resource = this.processResource(link_info.getResource());
							this.knowledgeresources.put(key, resource);
							break;
						}
					}
				}
			}
			resource = this.knowledgeresources.get(key);
		}
		return resource;
	}
	
	private KnowledgeResource processResource(List<Resource> resources) {
		KnowledgeResource k_resources = null;
		k_resources = new KnowledgeResource();
		for(Resource resource: resources) {
			
			if(resource.getName().equalsIgnoreCase("utube")) {
				k_resources.setUtubeResources(resource.getItem());
			}
			else if(resource.getName().equalsIgnoreCase("web")) {
				k_resources.setWebResources(resource.getItem());
			}
		}
		return k_resources;
	}
	


}



